Skip to content

Fix very slow jsdoc search in chained special assignments - #23115

Merged
Nathan Shively-Sanders (sandersn) merged 1 commit into
masterfrom
fix-factorial-jsdoc-search
Apr 4, 2018
Merged

Nathan Shively-Sanders (sandersn) merged 1 commit into
masterfrom
fix-factorial-jsdoc-search

Conversation

@sandersn

Copy link
Copy Markdown
Member

Fixes #23111

The fix is to only recur once per parent instead of twice. This means moving the chained-assignment check in a slightly less obvious place.

Comment thread src/compiler/utilities.ts
@@ -1807,7 +1807,6 @@ namespace ts {
if (parent &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking if parent is defined over and over again, maybe you should bail out if parent isn't defined?

@sandersn

Nathan Shively-Sanders (sandersn) commented Apr 3, 2018 via email •

Copy link
Copy Markdown
Member Author

@sandersn

Copy link
Copy Markdown
Member Author

Daniel Rosenwasser (@DanielRosenwasser) here's what I got from de-duping the repeated parent checks. If you like it I can put it in a separate PR after this one:

if (parent) {
    if(parent.kind === SyntaxKind.PropertyAssignment ||
       parent.kind === SyntaxKind.PropertyDeclaration ||
       getNestedModuleDeclaration(parent) ||
       isBinaryExpression(node) && getSpecialPropertyAssignmentKind(node) ||
       isBinaryExpression(parent) && getSpecialPropertyAssignmentKind(parent) ||
       node.kind === SyntaxKind.PropertyAccessExpression && parent.kind === SyntaxKind.ExpressionStatement) {
        getJSDocCommentsAndTagsWorker(parent);
    }
    if (parent.parent) {
        // Try to recognize this pattern when node is initializer of variable declaration and JSDoc comments are on containing variable statement.
        // /**
        //   * @param {number} name
        //   * @returns {number}
        //   */
        // var x = function(name) { return name.length; }
        if (getSingleVariableOfVariableStatement(parent.parent) === node || getSourceOfAssignment(parent.parent)) {
            getJSDocCommentsAndTagsWorker(parent.parent);
        }
        if (parent.parent.parent) {
            if (getSingleInitializerOfVariableStatementOrPropertyDeclaration(parent.parent.parent) === node || getSourceOfDefaultedAssignment(parent.parent.parent)) {
                getJSDocCommentsAndTagsWorker(parent.parent.parent);
            }
        }
    }
}

Note that I removed the initial indent that comes from context, so the lines are actually longer than this in the editor.

@ghost

Copy link
Copy Markdown

Nathan Shively-Sanders (@sandersn) Since this always ends in a call to getJSDocCommentsAndTagsWorker, maybe you could factor out a function getJSDocCommentsAndTagsHost that returns parent or parent.parent or parent.parent.parent or undefined.

@sandersn
Nathan Shively-Sanders (sandersn) deleted the fix-factorial-jsdoc-search branch April 4, 2018 15:57
@sandersn

Copy link
Copy Markdown
Member Author

Andy (Andrewkraft) (@Andy-MS) I tried this -- is it what you were proposing?

    function getJSDocCommentsAndTagsHost(node: Node) {
        return node.parent
            ? node.parent.parent
                ? node.parent.parent.parent ? node.parent.parent.parent : node.parent.parent
            : node.parent
        : undefined;
    }
        function getJSDocCommentsAndTagsWorker(node: Node): void {
            const host = getJSDocCommentsAndTagsHost(node);
            if (host &&
                (host.kind === SyntaxKind.PropertyAssignment ||
                 host.kind === SyntaxKind.PropertyDeclaration ||
                 getNestedModuleDeclaration(host) ||
                 getSingleVariableOfVariableStatement(host) === node ||
                 getSourceOfAssignment(host) ||
                 getSingleInitializerOfVariableStatementOrPropertyDeclaration(host) === node ||
                 getSourceOfDefaultedAssignment(host) ||
                 isBinaryExpression(node) && getSpecialPropertyAssignmentKind(node) !== SpecialPropertyAssignmentKind.None ||
                 isBinaryExpression(host) && getSpecialPropertyAssignmentKind(host) !== SpecialPropertyAssignmentKind.None ||
                 node.kind === SyntaxKind.PropertyAccessExpression && host.kind === SyntaxKind.ExpressionStatement)) {
                getJSDocCommentsAndTagsWorker(host);
            }
// ......

This doesn't work as-is, but might be close.

@ghost

Copy link
Copy Markdown

Working on it.

@ghost

Copy link
Copy Markdown

Never mind, the function needs to recurse on itself up to three times, my suggesstion would only work if it only needed to recurse once.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In JS, deeply chained exports assignments cause long compile time

2 participants